import React, { Component } from 'react';
import './JudgeTable.css';
import JudgeRow from './JudgeRow';


/**
 * tableType 表格类型 ny wy
 * judgeDate 表格的标准考核原始数据
 * judgeTableName 表格名称
 * markScores 表格打分情况
 * canMark 是否可以打分，默认为否
 * commitScore 提交打分
 */

const styles = {
  tableTHeadTh: {
    border: '1px solid #464646',
    width: '50px'
  },
  tableTHeadTd: {
    border: '1px solid #464646'
  }
};
class JudgeTable extends Component {


  renderTableHeader = () => {
    const { tableType } = this.props;
    if (tableType === 'wy') {
      return <tr>
        <th style={styles.tableTHeadTh}>序号</th>
        <th style={styles.tableTHeadTh}>类别</th>
        <th style={styles.tableTHeadTh}>考核项目</th>
        <th style={styles.tableTHeadTh}>考核方法</th>
        <th style={styles.tableTHeadTh}>考核内容</th>
        <th style={styles.tableTHeadTh}> 扣分标准</th>
        <th style={styles.tableTHeadTh}>标准分</th>
        <th style={styles.tableTHeadTh}>实得分</th>
      </tr>
    } else {
      return <tr>
        <th style={styles.tableTHeadTh}>序号</th>
        <th style={styles.tableTHeadTh}>类别</th>
        <th style={styles.tableTHeadTh}>考核方法</th>
        <th style={styles.tableTHeadTh}>考核内容</th>
        <th style={styles.tableTHeadTh}>扣分标准</th>
        <th style={styles.tableTHeadTh}>标准分</th>
        <th style={styles.tableTHeadTh}>实得分</th>
      </tr>
    }
  };

  _handleChangeScore = (scores, judgeClassify) => {
    this.props.commitScore(this.props.tableClassify, judgeClassify, scores);
  };

  render() {
    const { tableType, judgeDate, judgeTableName, markScores } = this.props;

    let WyJudgeDate = [];

    let renderJudgeDate;

    if (tableType === 'wy') {
      judgeDate.map((item) => {

        item.children.map((child, index) => {
          let row = {};
          if (index === 0) {
            row.name = item.name;
            row.rowspan = item.children.length;
          }
          row.classify = child.name;
          row.judgeMethod = child.judgeMethod;
          row.standardScore = child.standardScore;
          row.content = child.content;
          row.judgeItems = child.judgeItems;
          WyJudgeDate.push(row);
          return null;
        });
        return null;
      });
      renderJudgeDate = WyJudgeDate;

    } else {
      renderJudgeDate = judgeDate;
    }


    //计算应得分
    let standardScoreTotal = 0;
    let realScoreTotal = 0;
    Object.keys(markScores).map(key => {
      if (markScores[key].isJudge) {
        standardScoreTotal += markScores[key].standardScore;
        realScoreTotal += parseFloat(markScores[key].markScore);
      }
      return null;
    });

    return (
      <div>
        {tableType === 'wy' ? <div style={{ textAlign: 'center',fontSize:'24px',fontWeight:'700' }}> 表2 施工单位外业部分考核评价表</div> : null}
        <div style={{ textAlign: 'center' ,fontSize:'18px',fontWeight:'600'}}>{judgeTableName}</div>
        <table style={{ borderCollapse: "collapse" }}>
          <thead>
          {this.renderTableHeader()}
          </thead>
          <tbody>
          {
            renderJudgeDate.map((item, index) => <JudgeRow commitScore={this._handleChangeScore} row={item} key={index}
                                                           canMark={this.props.canMark}
                                                           markScore={tableType === 'wy' ? markScores[item.classify] : markScores[item.name]}
                                                           index={index}/>)
          }
          <tr>
            <td style={styles.tableTHeadTd} colSpan={tableType === 'ny' ? 2 : 3}>标准分</td>
            <td style={styles.tableTHeadTd} colSpan={2}>{standardScoreTotal}</td>
            <td style={styles.tableTHeadTd} colSpan={2}>实得分</td>
            <td style={styles.tableTHeadTd}>{realScoreTotal ? realScoreTotal : 0}</td>
          </tr>
          </tbody>
        </table>
        <br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/>  <br/><br/><br/><br/><br/>
        <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/><br/> <br/>

      </div>);

  }
}

export default JudgeTable;